/*******************************************************************************
 * BIP MAIN VIEW
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * The GUI definition of the bip application
 */
qx.Class.define("bip.MainView", {
	extend : qx.ui.core.Widget,
	
	include : [
	           qx.ui.core.MRemoteChildrenHandling
	           
	         ],

	statics : {
		readyIconPath : "icon/16/status/dialog-information.png",
		warningIconPath : "icon/16/status/dialog-warning.png",
		errorIconPath : "icon/16/status/dialog-error.png",
		runningIconPath : "bip/icon/loading.gif",
		okIconPath : "icon/16/actions/dialog-apply.png",
		logoutIconPath : "bip/icon/aim_offline.png"
	},

	/*
	 * ****************************************************************************
	 * CONSTRUCTOR
	 * ****************************************************************************
	 */

	construct : function() {
		this.base(arguments);

		 // initialize the layout
    var layout = new qx.ui.layout.VBox();
    this._setLayout(layout);

		// Header
    this._createChildControl("header-title");
    this._createChildControl("header-local");
    this._createChildControl("header-user");
    this._createChildControl("header-state");
    this._createChildControl("header-logout");
    this._createChildControl("toolbar");
    this._createChildControl("portlets");
    this._createChildControl("pane");
    

	},

	/*
	 * ****************************************************************************
	 * PROPERTIES
	 * ****************************************************************************
	 */

	properties : {
		// overridden
		appearance : {
			refine : true,
			init : "application"
		},
		
		state : {
			check : [ "ready", "running", "warning", "error", "ok", "unregistered" ],
			init : "ready",
			apply : "__applyState",
			event : "changeState"
		},
		
		/** The header title */
		headerTitle : {
			apply : "_applyHeaderTitle",
			check : "String",
			init : "BIP"
		},
		
		/** The user name */
		headerUser : {
			apply : "_applyHeaderUser",
			check : "String",
			nullable: true
		}
		
	},

	/*
	 * ****************************************************************************
	 * MEMBERS
	 * ****************************************************************************
	 */

	members : {
		
		/*
		 * ****************************************************************************
		 * UI
		 * ****************************************************************************
		 */
		// overridden
		_createChildControlImpl : function(id) {
			var control;
			switch (id) {
			
				case "header":
					var layout = new qx.ui.layout.Grid();
          layout.setRowFlex(0, 1);
          layout.setColumnFlex(1, 1);
          control = new qx.ui.container.Composite(layout);
          this._add(control);
					break;

				case "header-title":
					control = new qx.ui.basic.Atom("BIP");
					control.setMarginLeft(5);
					this.getChildControl("header").add(control,{row:0,column:0});
					break;

				case "header-state":
					control = new qx.ui.basic.Atom();
					this.getChildControl("header").add(control,{row:0,column:5});
					break;
					
				case "header-local":
					control = new bip.ui.form.LocalSelectBox();
					control.setWidth(0);
					this.getChildControl("header").add(control,{row:0,column:6});
					break;
					
				case "header-user":
					control = new qx.ui.basic.Atom();
					this.getChildControl("header").add(control,{row:0,column:7});
					break;
					
				case "header-logout":
					control = new qx.ui.toolbar.Button(null,this.self(arguments).logoutIconPath);
					
					control.addListener("execute", function(evt) {
						if (confirm(this.tr("Exit from the application ?"))) {
							try {
								window.location = DISCONNECT_URL;
							} catch(e) {
								alert("FATAL_ERROR, please contact administrator:"+e);
							}
						}
					});
					this.getChildControl("header").add(control,{row:0,column:10});
					break;
					
				
					
				case "toolbar":
					control = new qx.ui.toolbar.ToolBar();
					this.getChildControl("header").add(control,{row:1,column:0});
					break;
				
				case "split":
          control = new qx.ui.splitpane.Pane("horizontal"); 
          this._add(control, {flex: 1});
          break;
					
				case "pane":
					control = new qx.ui.window.Desktop();
          this.getChildControl("split").add(control, 1);
          break;
          
				case "portlets":
					control = new qx.ui.window.Desktop();
					control.setWidth(0);
          this.getChildControl("split").add(control);
          break;
          
			}

			return control || this.base(arguments, id);

		},
		
  	/*
    ---------------------------------------------------------------------------
      WIDGET API
    ---------------------------------------------------------------------------
    */
    // overridden
    getChildrenContainer : function() {
      return this.getChildControl("pane");
    },
    
    /*
    ---------------------------------------------------------------------------
      PROPS HANDLERS
    ---------------------------------------------------------------------------
    */
		__applyState : function(value, old) {
			this.getChildControl("header-user").setLabel(this.getHeaderUser());
			this.getChildControl("header-logout").setEnabled(true);
			switch (value) {
				case "unregistered":
					this.getChildControl("header-user").setLabel("unregistered");
					this.getChildControl("header-logout").setEnabled(false);
					break;
				default:
					
					break;
			}
		},
		
		// User
		_applyHeaderUser : function(value, old) {
			this.getChildControl("header-user").setLabel(value);
		}
	},

	/*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */

	destruct : function() {
		this._disposeObjects();
	}
});
